﻿Public Class Users
    Public Shared UsersDataSet As DataSet = New DataSet
    Dim DatabaseFuncs As New DatabaseFuncs
    Dim LoginService As New LoginService
    'Variabelen om een gebruiker op te halen:
    Public _uId As Integer
    Public ReadOnly Property uId() As Integer
        Get
            uId = _uId
        End Get
    End Property

    Private _Unique_Id As String
    Public ReadOnly Property Unique_Id() As String
        Get
            Unique_Id = _Unique_Id
        End Get
    End Property

    Private _name As String
    Public Property name() As String
        Get
            name = _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Private _ovnummer As String
    Public Property ovnummer() As String
        Get
            ovnummer = _ovnummer
        End Get
        Set(ByVal value As String)
            _ovnummer = value
        End Set
    End Property

    Private _encrypted_password As String
    Public Property encrypted_password() As String
        Get
            encrypted_password = _encrypted_password
        End Get
        Set(ByVal value As String)
            _encrypted_password = value
        End Set
    End Property

    Private _salt As String
    Public Property salt() As String
        Get
            salt = _salt
        End Get
        Set(ByVal value As String)
            _salt = value
        End Set
    End Property

    Private _created_at As String
    Public Property created_at() As String
        Get
            created_at = _created_at
        End Get
        Set(ByVal value As String)
            _created_at = value
        End Set
    End Property

    Private _updated_at As String
    Public Property updated_at() As String
        Get
            updated_at = _updated_at
        End Get
        Set(ByVal value As String)
            _updated_at = value
        End Set
    End Property

    Private _level As Integer
    Public Property level() As Integer 'User level. 0 = leerling, 2 = docent, 3 = superadmin
        Get
            level = _level
        End Get
        Set(ByVal value As Integer)
            _level = value
        End Set
    End Property

    Private _resetpassword As Integer
    Public Property resetpassword() As Integer '0 / 1
        Get
            resetpassword = _resetpassword
        End Get
        Set(ByVal value As Integer)
            _resetpassword = value
        End Set
    End Property

    Private _klas As String
    Public Property klas() As String
        Get
            klas = _klas
        End Get
        Set(ByVal value As String)
            _klas = value
        End Set
    End Property


    Private _disabled As Boolean
    Public Property disabled() As Boolean
        Get
            disabled = _disabled
        End Get
        Set(ByVal value As Boolean)
            _disabled = value
        End Set
    End Property


    Public Function getDate()
        Dim regDate As Date = Date.Now()
        Dim Datum As String = regDate.ToString("dd-MM-yyyy")
        Return Datum

    End Function

    Public Sub LoadFromDB(ByVal ovnr As String, Optional ByVal naam As String = "") 'OVnummer is nu string, omdat beheerders met hun drielettercode kunnen inloggen.
        Try
            Dim queryresult As String = ""
            'User ophalen d.m.v. ovnummer.
            If Not naam = "" Then
                queryresult = DatabaseFuncs.query("SELECT * FROM `users` WHERE `name`='" + naam.ToString + "'", 2)
            Else
                queryresult = DatabaseFuncs.query("SELECT * FROM `users` WHERE `ovnummer`='" + ovnr.ToString + "'", 2)
            End If
            If Not queryresult = "True" Then 'De query heeft een fout.
                MsgBox("Laden informatie uit Usersdatabase is mislukt (fout vanuit query): " + queryresult)
                Exit Sub
            End If

            If Not UsersDataSet.Tables("Table").Rows.Count = 0 Then
                Dim DR As DataRow = UsersDataSet.Tables("Table").Rows(0)
                _uId = Convert.ToInt32(DR.Item("uid").ToString)
                _Unique_Id = DR.Item("unique_id").ToString
                name = DR.Item("name").ToString
                ovnummer = DR.Item("ovnummer").ToString
                encrypted_password = DR.Item("encrypted_password").ToString
                salt = DR.Item("salt").ToString
                created_at = DR.Item("created_at").ToString
                updated_at = DR.Item("updated_at").ToString
                klas = DR.Item("klas").ToString
                level = Convert.ToInt32(DR.Item("level").ToString)
                disabled = Convert.ToBoolean(DR.Item("disabled").ToString)
                resetpassword = Convert.ToInt16(DR.Item("resetpassword").ToString)
            End If

        Catch ex As Exception
            MsgBox("Laden informatie uit Usersdatabase is mislukt (fout bij variabelen): " + ex.Message)
        End Try
    End Sub

    Public Sub clearData()
        _uId = Nothing
        _Unique_Id = Nothing
        name = Nothing
        ovnummer = Nothing
        encrypted_password = Nothing
        salt = Nothing
        created_at = Nothing
        updated_at = Nothing
        level = Nothing
        disabled = Nothing
        klas = Nothing
        resetpassword = Nothing
    End Sub

    Public Function LoadAllFromDB()
        Dim queryresult As String = DatabaseFuncs.query("SELECT * FROM `users` ORDER BY `uid`", 2)
        If Not queryresult = "True" Then
            Return queryresult
        End If
        Return "True"
    End Function

    Public Function LogIn(ByVal username As String, ByVal password As String)
        LoadFromDB(username)

        If (encrypted_password = LoginService.MD5(password)) Then 'Het wachtwoord klopt
            If disabled = True Then
                Return "Geblokkeerd"
            End If
            If level > 1 Then
                Return "True"
            Else
                Return "Geenrechten"
            End If
        Else
            Return "Nietcorrect"
        End If

        Return "Nietcorrect"
    End Function

    Public Sub SaveToDB(ByVal id As String, ByVal NieuweUser As Boolean)
        If NieuweUser = True Then
            DatabaseFuncs.query("INSERT INTO `users` (name, ovnummer, encrypted_password, level, disabled, created_at, unique_id, klas, resetpassword) VALUES ('" + name.ToString + "', '" + ovnummer.ToString + "', '" + encrypted_password.ToString + "', " + level.ToString + ", " + disabled.ToString + ", '" + getDate() + "', '" + LoginService.MD5(name.ToString + ovnummer.ToString) + "', '" + klas.ToString + "', " + resetpassword.ToString + ")", 2) 'Het unieke ID moet uniek zijn, anders geeft het errors. Vandaar deze tussenoplossing van naam+ov.
        Else
            DatabaseFuncs.query("UPDATE `users` SET `name` = '" + name + "', `ovnummer` = '" + ovnummer + "', " _
            + "`encrypted_password` = '" + encrypted_password + "', `level` = " + level.ToString + ", `disabled` = " + disabled.ToString + ", `updated_at` = '" + getDate() + "', `klas` = '" + klas.ToString + "', `resetpassword` = " + resetpassword.ToString + " WHERE uid=" + uId.ToString, 2)
        End If
    End Sub

    Public Function DeleteFromDB(ByVal ovnummer As String)
        Dim queryresult As String = DatabaseFuncs.query("DELETE FROM `users` WHERE `ovnummer`='" + ovnummer + "'", 2)
        If Not queryresult = "True" Then
            Return queryresult
        End If
        clearData()
        Return "True"
    End Function

End Class
